package com.alili_tv.system.service.impl;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.entity.Comment;
import com.alili_tv.system.entity.User;
import com.alili_tv.system.mapper.CommentMapper;
import com.alili_tv.system.service.CommentService;
import com.alili_tv.system.service.LikesService;
import com.alili_tv.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-03-17
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserService userService;

    @Resource
    private LikesService likesService;

    @Override
    public List<Comment> getByVideoId(long video, int pageIndex, int pageSize, boolean isHot) {
        return commentMapper.getByVideoId(video, pageIndex, pageSize, isHot);
    }

    @Override
    public long getCountByVideoId(long videoId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getVideoId, videoId);
        return this.count(queryWrapper);
    }

    @Override
    public long getCountByParentId(long parentId) {
        LambdaQueryWrapper<Comment> sonWrapper = new LambdaQueryWrapper<>();
        sonWrapper.eq(Comment::getParentId, parentId);
        return this.count(sonWrapper);
    }

    @Override
    public List<Comment> getByParentId(Long parentId) {
        // 获取子评论信息
        return commentMapper.getByParentId(parentId);


//        // 获取子评论
//        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper
//                .eq(Comment::getParentId, parentId)
//                .orderByAsc(Comment::getCreateTime);
//        List<Comment> list = this.list(queryWrapper);
//
//        list = list.stream().peek((item) -> {
//            // 判断当前用户是否点赞此评论
//
//            item.setIsLike(this.isLike(item.getId()));
//
//            // 获取子评论的评论者信息
//            User user = userService.getById(item.getCreateUser());
//            item.setUser(user);
//
//            // 当子评论中还有相互评论, 只设置父评论者的昵称
//            if (item.getParentId() != null) {
//                String parentUserName = userService.getNameById(item.getParentId());
//                item.setParentUserName(parentUserName);
//            }
//        }).toList();
//
//        return list;
    }


    public boolean isLike(Long commentId) {
        return BaseContext.getCurrentId() != null && likesService.isLikeOfComment(BaseContext.getCurrentId(), commentId);
    }

    @Override
    public List<Comment> getByIds(Collection<Long> ids) {
        if (ids == null ||ids.size() == 0) {
            return null;
        }

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<Comment>()
                .in(Comment::getId, ids);
        return this.list(queryWrapper);
    }
}
